﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web;
using System.Web.Mvc;
using Com;
using Core;
using Models;

namespace Controllers
{
    public class HomeController:BaseController
    {
        public ActionResult Index()
        {
            var viewName = string.Empty;
            if (Request.Browser.IsMobileDevice)
            {
                viewName =string.IsNullOrEmpty(info.mskin) ? "" : info.mskin + "/index";
            }
            else
            {
                viewName =string.IsNullOrEmpty(info.skin) ? "" : info.skin + "/index";
            }
            var core=new infoCore();
            core.UpdateHits(info.ID);
            return View(viewName+ suffix);
        }

        [Route("List/{id}")]
        public ActionResult List(int id=0)
        {
            categoryCore core = new categoryCore();
            var model = core.GetOne(id);
            if (model.ID==0)
            {
                throw new HttpException(404,"页面未找到！");
            }
            //当前ID
            Pagemodel.ID = model.ID;
            //父类ID
            Pagemodel.classid = model.fid;
            //页面主题 分类名称
            Pagemodel.title = model.title;
            //次页banner
            Pagemodel.cbanner = GetCategoryCbanner(model, core);

            //模版路径
            var viewName = string.Empty;
            var pagename = GetCategorySkin(model, core);
            if (Request.Browser.IsMobileDevice)
            {
                viewName = string.IsNullOrEmpty(info.mskin) ? pagename : info.mskin + "/"+ pagename;
                //标题
                var pagetitle = string.IsNullOrEmpty(model.mpage_title) ? model.page_title : model.mpage_title;
                pagetitle = string.IsNullOrEmpty(pagetitle) ? info.pre_title + model.title + info.end_title : info.pre_title + pagetitle + info.end_title;
                Pagemodel.pagetitle = pagetitle;
                //关键词
                var keyword = string.IsNullOrEmpty(model.mpage_key) ? model.page_key : model.mpage_key;
                keyword = string.IsNullOrEmpty(keyword) ? Pagemodel.keywords : keyword;
                Pagemodel.keywords = keyword;
                //描述
                var description = string.IsNullOrEmpty(model.mpage_desc) ? model.page_desc : model.mpage_desc;
                description = string.IsNullOrEmpty(description) ? Pagemodel.description : description;
                Pagemodel.description = description;
                //页面内容
                Pagemodel.content = string.IsNullOrEmpty(model.mcontent) ? model.content : model.mcontent;
            }
            else
            {
                viewName = string.IsNullOrEmpty(info.skin) ? pagename : info.skin + "/" + pagename;
                //标题
                Pagemodel.pagetitle = string.IsNullOrEmpty(model.page_title) ? info.pre_title + model.title + info.end_title : info.pre_title + model.page_title + info.end_title;
                //关键词
                Pagemodel.keywords = string.IsNullOrEmpty(model.page_key) ? Pagemodel.keywords : model.page_key;
                //描述
                Pagemodel.description = string.IsNullOrEmpty(model.page_desc) ? Pagemodel.description : model.page_desc;
                //页面内容
                Pagemodel.content = model.content;
            }

            //设置当前位置
            var cur = GetCategoryCur(model, core);
            Pagemodel.cur = cur;
            //详细信息
            Pagemodel.info.Add("model",model);
            core.UpdateHits(id);
            return View(viewName + suffix);
        }

        [Route("Article/{id}")]
        public ActionResult Article(int id=0)
        {
            xinxiCore core = new xinxiCore();
            var model = core.GetQOne(id);
            if (model.ID==0)
            {
                throw new HttpException(404, "页面未找到！");
            }

            //当前ID
            Pagemodel.ID = model.ID;
            //父类ID
            Pagemodel.classid = model.classid;
            //页面主题 分类名称
            Pagemodel.title = model.title;  
            //设置当前位置
            categoryCore categorycore = new categoryCore();
            var cmodel = categorycore.GetOne(model.classid);
            var cur = GetArticleCur(model, cmodel);
            Pagemodel.cur = cur;
            //次页banner
            Pagemodel.cbanner = GetArticleCbanner(model, categorycore);

            //模版路径
            var viewName = string.Empty;
            var pagename = GetArticleSkin(model, categorycore);
            if (Request.Browser.IsMobileDevice)
            {
                viewName = string.IsNullOrEmpty(info.mskin) ? pagename : info.mskin + "/" + pagename;
                //标题
                var pagetitle = string.IsNullOrEmpty(model.mpage_title) ? model.page_title : model.mpage_title;
                pagetitle = string.IsNullOrEmpty(pagetitle) ? info.pre_title + model.title + info.end_title : info.pre_title + pagetitle + info.end_title;
                Pagemodel.pagetitle = pagetitle;
                //关键词
                var keyword = string.IsNullOrEmpty(model.mpage_key) ? model.page_key : model.mpage_key;
                keyword = string.IsNullOrEmpty(keyword) ? Pagemodel.keywords : keyword;
                Pagemodel.keywords = keyword;
                //描述
                var description= string.IsNullOrEmpty(model.mpage_desc) ? model.page_desc : model.mpage_desc;
                description = string.IsNullOrEmpty(description) ? Pagemodel.description : description;
                Pagemodel.description = description;
                //页面内容
                Pagemodel.content = string.IsNullOrEmpty(model.mcontent)?model.content:model.mcontent;
            }
            else
            {
                viewName = string.IsNullOrEmpty(info.skin) ? pagename : info.skin + "/" + pagename;
                //标题
                Pagemodel.pagetitle = string.IsNullOrEmpty(model.page_title) ? info.pre_title + model.title + info.end_title : info.pre_title + model.page_title + info.end_title;
                //关键词
                Pagemodel.keywords = string.IsNullOrEmpty(model.page_key) ? Pagemodel.keywords : model.page_key;
                //描述
                Pagemodel.description = string.IsNullOrEmpty(model.page_desc) ? Pagemodel.description : model.page_desc;
                //页面内容
                Pagemodel.content = model.content;
            }
         
            //详细信息
            Pagemodel.info.Add("model", model);
            
            core.UpdateHits(id);
            return View(viewName + suffix);
        }
        [Route("liuyan/{id}")]
        public ActionResult Liuyan(string id)
        {            
            string[] leixings = new string[] { "局长信箱", "意见建议", "网上投诉" };
            if (!leixings.Contains(id))
            {
                throw new HttpException(404, "页面未找到！");
            }
            var liuyancore = new liuyanCore();
            Pagemodel.title = id;
            //标题
            Pagemodel.pagetitle = info.pre_title + Pagemodel.title + info.end_title;
            return View("liuyan" + suffix);
        }
        [Route("ly")]
        [HttpPost]
        public ActionResult Ly()
        {
            var code = Request.Form["code"].ToString();
            if (Session["code"] == null || code.ToLower() != Session["code"].ToString().ToLower())
            {
                Com.Utils.ResetCode();
                return Content("<script>alert('验证码错误！');history.back();</script>", "text/html");
            }
            var model =new liuyanEntity();
            model.username = Request.Form["username"];
            model.tel = Request.Form["tel"];
            model.title = Request.Form["title"];
            model.isopen = Convert.ToBoolean(Request.Form["isopen"] == "1");
            model.email = Request.Form["email"];
            model.content = Request.Form["content"];
            model.addtime = DateTime.Now;
            model.leixing = Request.Form["leixing"];
            model.zt = false;
            var lycode = "";
            switch (model.leixing)
            {
                case "局长信箱":
                    lycode = "jzxx";
                    break;
                case "网上投诉":
                    lycode = "swts";
                    break;
                default:
                    lycode = "lyjy";
                    break;
            }
            if(Session!=null&& Session["lycg" + lycode]!=null&&Session["lycg" + lycode].ToString() == "ok")
            {
                Session.Remove("lycg" + lycode);
                return Redirect("/liuyan/"+model.leixing);
            }
                model.code = model.tel+"_"+Utils.GetCheckCode(10)+ "_"+ lycode;
            new liuyanCore().Save(model);
            Session["lycg" + lycode] = "ok";
            Pagemodel.content = model.code;
            Pagemodel.title = Request.Form["leixing"];
            //标题
            Pagemodel.pagetitle = info.pre_title + Pagemodel.title + info.end_title;
            return View("ly"+suffix);
        }
        [Route("lycx")]
        public ActionResult Lycx()
        {
            Pagemodel.title = "信息查询";
            //标题
            Pagemodel.pagetitle = info.pre_title + Pagemodel.title + info.end_title;
            return View("lycx" + suffix);
        }
        [Route("lylist/{id}")]
        public ActionResult Lylist(string id)
        {
            Pagemodel.content = id;
            Pagemodel.title = "信息查询结果";
            //标题
            Pagemodel.pagetitle = info.pre_title + Pagemodel.title + info.end_title;
            return View("lylist" + suffix);
        }
        [Route("toupiao/{id}")]
        public  ActionResult Toupiao(int id)
        {
            Pagemodel.ID = id;
            Pagemodel.title = "问卷调查";
            Pagemodel.pagetitle = info.pre_title + "问卷调查" + info.end_title;
            var cur = @"<a href=""/"" class=""home"">首页</a>";
            cur += string.Format(@" <a href=""{0}"" class=""two"">{1}</a>","javascript:;", "问卷调查");
            Pagemodel.cur = cur;
            return View("toupiao" + suffix);
        }
        [Route("tou")]
        [HttpPost]
        public ActionResult Tou()
        {
            var id = Request.QueryString["id"];
            Pagemodel.ID = Convert.ToInt32(id);
            Pagemodel.title = "问卷调查结果";
            Pagemodel.pagetitle = info.pre_title + "问卷调查结果" + info.end_title;
            var cur = @"<a href=""/"" class=""home"">首页</a>";
            cur += string.Format(@" <a href=""{0}"" class=""two"">{1}</a>", "javascript:;", "问卷调查结果");
            Pagemodel.cur = cur;
            var toupiaocore = new toupiaoCore();
            var list = Request.Form.AllKeys;
            foreach (var item in list)
            {
                if (Session != null && Session["toupiao" + item] != null && Session["toupiao" + item].ToString() == "ok")
                {
                    return Redirect("/toupiao/" + id);
                }
                var toupiao = Request.Form[item];
                var model = toupiaocore.GetOne(Convert.ToInt32(toupiao));
                model.piao = model.piao + 1;
                toupiaocore.Save(model);
                Session["toupiao" + item] = "ok";
            }
            return View("tou" + suffix);
        }
        [Route("search/{keyword}")]
        public ActionResult Serach(string keyword="")
        {
            xinxiCore core = new xinxiCore();
            var model = core.GetQOneByTitle("搜索中心");
            if (model.ID==0)
            {
                model.title = "搜索中心";
            }

            //当前ID
            Pagemodel.ID = model.ID;
            //父类ID
            Pagemodel.classid = model.classid;
            //页面主题 搜索的关键词
            Pagemodel.title = keyword;
            //设置当前位置
            categoryCore categorycore = new categoryCore();
            var cmodel = categorycore.GetOne(model.classid);
            var cur = GetArticleCur(model, cmodel);
            Pagemodel.cur = cur;
            //模版路径
            var viewName = string.Empty;
            var pagename = (string.IsNullOrEmpty(model.skin) ? "search" : model.skin);
            if (Request.Browser.IsMobileDevice)
            {
                viewName = string.IsNullOrEmpty(info.mskin) ? pagename : info.mskin + "/" + pagename;
                //标题
                var pagetitle = string.IsNullOrEmpty(model.mpage_title) ? model.page_title : model.mpage_title;
                pagetitle = string.IsNullOrEmpty(pagetitle) ? info.pre_title + model.title + info.end_title : info.pre_title + pagetitle + info.end_title;
                Pagemodel.pagetitle = pagetitle;
                //关键词
                var keywords = string.IsNullOrEmpty(model.mpage_key) ? model.page_key : model.mpage_key;
                keywords = string.IsNullOrEmpty(keywords) ? Pagemodel.keywords : keywords;
                Pagemodel.keywords = keywords;
                //描述
                var description = string.IsNullOrEmpty(model.mpage_desc) ? model.page_desc : model.mpage_desc;
                description = string.IsNullOrEmpty(description) ? Pagemodel.description : description;
                Pagemodel.description = description;
                //页面内容
                Pagemodel.content = string.IsNullOrEmpty(model.mcontent) ? model.content : model.mcontent;
            }
            else
            {
                viewName = string.IsNullOrEmpty(info.skin) ? pagename : info.skin + "/" + pagename;
                //标题
                Pagemodel.pagetitle = string.IsNullOrEmpty(model.page_title) ? info.pre_title + model.title + info.end_title : info.pre_title + model.page_title + info.end_title;
                //关键词
                Pagemodel.keywords = string.IsNullOrEmpty(model.page_key) ? Pagemodel.keywords : model.page_key;
                //描述
                Pagemodel.description = string.IsNullOrEmpty(model.page_desc) ? Pagemodel.description : model.page_desc;
                //页面内容
                Pagemodel.content = model.content;

            }
            //详细信息
            Pagemodel.info.Add("model", model);
            return View(viewName + suffix);
        }

        #region 私有方法
        private string GetCategoryCur(categoryEntity model, categoryCore core)
        {
            var cur = @"<a href=""/"" class=""home"">首页</a>";
            if (model.fid > 0)
            {
                var fmodel = core.GetOne(model.fid);
                cur += string.Format(@" <a href=""{0}"" class=""one"">{1}</a>", Url.Action("list", new { fmodel.ID }), fmodel.title);
            }

            cur += string.Format(@" <a href=""{0}"" class=""two"">{1}</a>", Url.Action("list", new { model.ID }), model.title);
            return cur;
        }

        private string GetCategoryCbanner(categoryEntity model, categoryCore core)
        {
            var result = model.bannerpic;
            var cmodel = core.GetOne(model.fid);
            if (string.IsNullOrEmpty(result))
            {
                result = cmodel.bannerpic;
            }
            if (string.IsNullOrEmpty(result) && cmodel.fid > 0)
            {
                result = core.GetOne(cmodel.fid).bannerpic;
            }
            if (string.IsNullOrEmpty(result))
            {
                result = Pagemodel.cbanner;
            }
            return result;
        }
        private string GetCategorySkin(categoryEntity model, categoryCore core)
        {
            var result = model.skin;
            var cmodel = core.GetOne(model.fid);
            if (string.IsNullOrEmpty(result))
            {
                result = cmodel.skin;
            }
            if (string.IsNullOrEmpty(result) && cmodel.fid > 0)
            {
                result = core.GetOne(cmodel.fid).skin;
            }
            if (string.IsNullOrEmpty(result))
            {
                result = "list";
            }
            return result;
        }

        private string GetArticleSkin(qxinxiModel model, categoryCore core)
        {
            var result = model.skin;
            var cmodel = core.GetOne(model.classid);
            if (string.IsNullOrEmpty(result))
            {
                result = cmodel.pageskin;
            }
            if (string.IsNullOrEmpty(result) && cmodel.fid > 0)
            {
                result = core.GetOne(cmodel.fid).pageskin;
            }
            if (string.IsNullOrEmpty(result) && core.GetOne(cmodel.fid).fid > 0)
            {
                result = core.GetOne(core.GetOne(cmodel.fid).fid).pageskin;
            }
            if (string.IsNullOrEmpty(result))
            {
                result = "article";
            }
            return result;
        }
        private string GetArticleCbanner(qxinxiModel model, categoryCore core)
        {
            var result = model.bannerpic;
            var cmodel = core.GetOne(model.classid);
            if (string.IsNullOrEmpty(result))
            {
                result = cmodel.bannerpic;
            }
            if (string.IsNullOrEmpty(result) && cmodel.fid > 0)
            {
                result = core.GetOne(cmodel.fid).bannerpic;
            }
            if (string.IsNullOrEmpty(result) && core.GetOne(cmodel.fid).fid > 0)
            {
                result = core.GetOne(core.GetOne(cmodel.fid).fid).bannerpic;
            }
            if (string.IsNullOrEmpty(result))
            {
                result = Pagemodel.cbanner;
            }
            return result;
        }
        private string GetArticleCur(qxinxiModel model, categoryEntity fmodel)
        {
            var cur = @"<a href=""/"" class=""home"">首页</a>";
            if (model.classid > 0)
            {
                cur += string.Format(@" <a href=""{0}"" class=""one"">{1}</a>", Url.Action("list", new { fmodel.ID }), fmodel.title);
            }

            cur += string.Format(@" <a href=""{0}"" class=""two"">{1}</a>", Url.Action("article", new { model.ID }), model.title);
            return cur;
        }
        #endregion
    }
}
